package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@Transactional
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Autowired
    private DishMapper dishMapper;
    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setMealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = setMealMapper.getById(id);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.update(setmeal);
        //获取套餐id
        Long id = setmealDTO.getId();
        //根据套餐id删除套餐对应的菜品关系
        setMealDishMapper.deleteBySetmealId(id);
        //将id放在菜品上循环
        setmealDTO.getSetmealDishes().forEach(dish -> dish.setSetmealId(id));
        setMealDishMapper.insertBatch(setmealDTO.getSetmealDishes());

    }


    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setMealMapper.getBySetMealId(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                // 启动中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        for (Long id : ids) {
            //删除套餐表中的数据
            setMealMapper.deleteById(id);
            //删除套餐和菜品的关联数据
            setMealMapper.deleteById(id);
        }
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.insert(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(
                setmealDish ->  {setmealDish.setSetmealId(setmeal.getId());
                    System.out.println("sjdghhjsgdsjhgdghjsghjkdhj");}
        );
        setMealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void update(Integer status, Long id) {
        //起售时判断套餐内是否有停售的菜品
        if (status == StatusConstant.ENABLE) {
            List<Dish> list = dishMapper.getBySetMealId(id);
            if (list != null && list.size() > 0) {
                for (Dish dish : list) {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        // 停售的菜品不能启售
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }

        Setmeal setmeal = new Setmeal().builder()
                .status(status)
                .id(id)
                .build();
        setMealMapper.update(setmeal);
    }
}
